package com.gulimall.product.web;

import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author djg
 * @date 2020/7/30 16:00
 * @des
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model) {
        //1、查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    //index/catalog.json
    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        //1、获取一把锁，只要锁名一样，就是同一把锁
        RLock lock = redissonClient.getLock("my-lock");

        //2、加锁
        lock.lock(10, TimeUnit.SECONDS); //阻塞式等待
        try {
            System.out.println("加锁成功。。。" + Thread.currentThread().getId());
            Thread.sleep(1000);
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功。。。" + Thread.currentThread().getId());
            //3、解锁
            lock.unlock();
        }
        return "hello";
    }


    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.trySetPermits(3);
        park.acquire();//获取一个信号，获取一个值，占一个车位
//        boolean b = park.tryAcquire();

        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();//释放一个车位

        return "ok";
    }


    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了，人都走完了，锁门";
    }

    @GetMapping("/gogogo")
    @ResponseBody
    public String gogogo(Long id) {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown(); //计数 -1

        return id + "班的人走了";
    }
}
